
const app = getApp()

Page({
  data: {
    pageNum:1,
    indicatorDots: true,
    indicatorColor: "#aliceblue",
    indicatorActiveColor: "#f3f3f3",
    autoplay: true,
    interval: 3000,
    duration: 1000,
    showNotice: false, // 通知公告弹窗
    swiper_list: [],
    new_list: [],
    heat_list: [],
    box_list: [],
    activeList: [], // 活动公告数组
    activeShow: false,
    overlay: false, // 是否点击遮罩层可关闭弹窗
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow(){
    this.setData({
      swiper_list: [],
      new_list: [],
      heat_list: [],
      box_list: []
    })
    this.getSwiperList()
    this.getNewList()
    this.getHeatList()
    this.getBoxList()
  },
  onLoad() {
    wx.showShareMenu({
      withShareTicket: true,
    })
    this.sendSubMessage()
  },
  onShareAppMessage() {

  },
  onShareTimeline() {
    return {
      title: '梦与玩',
      query: '/pages/index/index'
    }
  },
  sendSubMessage() {
    let _this = this;
    wx.getSetting({
      withSubscriptions: true,
      success (res) {
        console.log(res.subscriptionsSetting)
        if (res.subscriptionsSetting.itemSettings) {
          let itemSettings = res.subscriptionsSetting.itemSettings;
          if (itemSettings['AF6EtQkRCcpZcJ_EwsaJXKBGumQVl2CbZdAa5XyLi18'] == 'reject' || !itemSettings['AF6EtQkRCcpZcJ_EwsaJXKBGumQVl2CbZdAa5XyLi18']) {
            console.log('reject')
            _this.onTake()
          } else {
            _this.getNoticeList()
          }
        } else {
          _this.onTake()
        }
      }
    })
},
onTake() {
  let _this = this;
  wx.showModal({
    title: '温馨提示',
    content: '为更好的促进您与平台之间的交流，服务号需要在您寄售，求购，交换，提现等操作后向您发送消息。',
    success(res) {
      if (res.confirm) {
        wx.requestSubscribeMessage({
          tmplIds: ['AF6EtQkRCcpZcJ_EwsaJXKBGumQVl2CbZdAa5XyLi18','-KLyvHKusvlznii1TeZozEJIR24U4zcg7wcZnAbjR5g', '6Xzgk7faA1bWZ25EUlztDV4ZflRlzgsM6wlRV8jJpcU'],
          success (res) {
            console.log(res)
            if (res['AF6EtQkRCcpZcJ_EwsaJXKBGumQVl2CbZdAa5XyLi18'] == 'accept') {
              wx.showToast({
                title: '订阅成功！',
                icon: 'success',
                duration: 1500
              })
            } else {

              wx.showModal({
                title: '提示',
                content: res.errMsg,
                showCancel: false,
                confirmText: '我知道了'
              })
              // wx.showToast({
              //   title: res.errMsg,
              //   icon: 'none',
              //   duration: 1500
              // })
            }
            
            _this.getNoticeList()
          }, fail(err) {
            if (err.errCode == 20004) {
              wx.showModal({
                title: '温馨提示',
                content: '请先打开小程序的订阅总开关再尝试！',
                showCancel: false,
                confirmText: '我知道了'
              })
            } else {
              wx.showModal({
                title: '温馨提示',
                content: err.errMsg,
                showCancel: false,
                confirmText: '我知道了'
              })
            }
            console.log(err)
            
            _this.getNoticeList()
          }
        })
      } else {
        _this.getNoticeList()
      }
    }
  })
},
  // 获取活动公告
  getNoticeList() {
    let _this = this;
    app.util.request({
      url: "noLogin/getActivityPage",
      method: "post",
      success(response) {
        if (response.data.code == 0) {
          let arr = response.data.data.records;
          if (arr.length > 0) {
            _this.setData({
              activeShow: true,
              activeList: arr
            })
          } else {
            _this.setData({
              activeShow: false,
              activeList: []
            })
          }
        }
      }
    })
  },
  // 关闭活动公告弹窗
  onClose() {
    this.setData({
      activeShow: false
    })
  },
  toActive(e) {
    let url = e.currentTarget.dataset.item.skipUrl
    wx.navigateTo({
      url: url,
    })
  },
  closeNotice() {
    let that = this
    that.setData({
      showNotice: false
    })
  },
  getSwiperList() {
    let _this = this;
    app.util.request({
      url: "api/banner/findall",
      method: "get",
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            swiper_list: response.data.data.list
          })
        }
      }
    })
  },
  getNewList() {
    let _this = this;
    app.util.request({
      url: "api/mall/findPresell",
      method: "get",
      data: {
        page: 1,
        pageSize: 10
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            new_list: response.data.data.list
          })
        }
      }
    })
  },
  getHeatList() {
    let _this = this;
    app.util.request({
      url: "api/mall/findSales",
      method: "get",
      data: {
        page: 1,
        pageSize: 10
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            heat_list: response.data.data.list
          })
        }
      }
    })
  },
  // 去热卖详情
  toHotDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../market/market-detail/market-detail?id='+ id + "&goodstype=0",
    })
  },
  getBoxList() {
    let _this = this;
    app.util.request({
      url: "api/series/findLimit",
      method: "post",
      data: {
        status:1,
        page: _this.data.pageNum,
        pageSize: 4,
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            box_list: response.data.data.list
          })
        }
      }
    })
  },
  toAllPage(item) {
    console.log(111)
      wx.navigateTo({
        url: `/pages/index/index-detail/index-detail?type=${item.detail}`,
      })
  },
  toBoxDetail(e) {
    app.box_icon= this.data.box_list[e.currentTarget.dataset.index].icon
    wx.navigateTo({
      url: '/pages/boxes/blind-boxes?id=' + e.currentTarget.dataset.id,
    })
  },
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/market/market-detail/market-detail?id=' + e.currentTarget.dataset.id + '&goodstype=' + e.currentTarget.dataset.goodstype,
    })
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '展示用户信息', 
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
})
